Skip to content

Commit 4fb3cb1

Browse files
committed
Fix topic inheritance for translated documents
This commit addresses issues with topic handling for translated documents: 1. Adds signal handler to automatically propagate topic changes from parent documents to their translations, ensuring database consistency 2. Improves query filtering in facets.py to include translated documents based on their parent's topics, providing correct results even before database synchronization occurs These changes ensure that topics are consistently applied across all translations of a document, both at the database level and in query results.
1 parent 0013f8a commit 4fb3cb1

File tree

4 files changed

+28
-1
lines changed

4 files changed

+28
-1
lines changed

kitsune/wiki/apps.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ class WikiConfig(AppConfig):
66
default_auto_field = "django.db.models.AutoField"
77

88
def ready(self):
9+
# Import signals to register them
10+
from kitsune.wiki import signals # noqa: F401 - import for signal registration
911
from kitsune.wiki.badges import register_signals
1012

1113
# register signals for badges

kitsune/wiki/facets.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,9 @@ def _documents_for(user, locale, topics=None, products=None):
112112
if topics:
113113
topic_ids = [t.id for t in topics]
114114
# we need to filter against parent topics for localized articles
115-
qs = qs.filter(Q(topics__in=topic_ids) | Q(parent__topics__in=topic_ids))
115+
qs = qs.filter(
116+
Q(topics__in=topic_ids) | Q(parent__isnull=False, parent__topics__in=topic_ids)
117+
)
116118
for product in products or []:
117119
# we need to filter against parent products for localized articles
118120
qs = qs.filter(Q(products=product) | Q(parent__products=product))

kitsune/wiki/models.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,18 @@ def is_linked_in_product(self):
797797
locale__in=("", self.locale), target__contains=f"kb/{self.slug}"
798798
).exists()
799799

800+
def propagate_topics_to_translations(self):
801+
"""Propagate topic changes from a parent document to its translations."""
802+
if self.locale != settings.WIKI_DEFAULT_LANGUAGE:
803+
# Only operate on parent documents
804+
return
805+
806+
parent_topics = list(self.topics.all())
807+
for translated_doc in self.translations.all():
808+
# Clear existing topics and add parent's topics
809+
translated_doc.topics.clear()
810+
translated_doc.topics.add(*parent_topics)
811+
800812

801813
class AbstractRevision(models.Model):
802814
# **%(class)s** is being used because it will allow a unique reverse name for the field

kitsune/wiki/signals.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from django.db.models.signals import m2m_changed
2+
from django.dispatch import receiver
3+
4+
from kitsune.wiki.models import Document
5+
6+
7+
@receiver(m2m_changed, sender=Document.topics.through)
8+
def handle_document_topics_change(sender, instance, action, **kwargs):
9+
"""When a document's topics change, propagate to translations if needed."""
10+
if action in ("post_add", "post_remove", "post_clear"):
11+
instance.propagate_topics_to_translations()

0 commit comments

Comments
 (0)